package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.auth.AuthCacheService;
import com.hbx.auth.exception.AuthException;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.RspEnum;
import com.hbx.constant.TableEnum;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.logs.OperaTypeEnum;
import com.hbx.dto.menu.UpdateDto;
import com.hbx.exception.RspException;
import com.hbx.model.User;
import com.hbx.service.MenuService;
import com.hbx.utils.Result;
import com.hbx.vo.login.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2021-11-26
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单处理器")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取用户菜单
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获得用户菜单")
    public Result<MenuVo> list() {
        // 获得当前登录用户
        User user = AuthCacheService.getUser();
        if (ObjectUtils.isEmpty(user)) {
            throw new AuthException(RspEnum.AUTH_NO_LOGIN);
        }
        return menuService.getMenuByUserId(user.getUserId());
    }

    /**
     * 获取所有菜单
     *
     * @return
     */
    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
    @ApiOperation("获取所有菜单")
    public Result getMenuList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        if (pageNum == null || pageNum < 0) {
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR, "pageNum传递有误");
        }
        if (pageSize == null || pageSize < 0) {
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR, "pageSize传递有误");
        }
        return menuService.getMenuList(pageNum, pageSize);
    }

    /**
     * 获取所有一二菜单
     *
     * @return
     */
    @RequestMapping(value = "/getMenuParentIdList", method = RequestMethod.GET)
    @ApiOperation("获取所有一二菜单菜单")
    public Result getMenuParentIdList() {
        return menuService.getMenuParentIdList();
    }

    /**
     * 修改菜单信息
     *
     * @param updateDto
     * @param result
     * @return
     */
    @RequestMapping(value = "/updateMenu", method = RequestMethod.POST)
    @ApiOperation("添加或修改菜单")
    @MarkLog(operate = OperaTypeEnum.UPDATE, tables = TableEnum.SYS_MENU, desc = "管理员添加或修改菜单信息")
    @SensitiveFilter
    public Result updateMenu(@RequestBody @Valid UpdateDto updateDto, BindingResult result) {
        Result r = new Result();
        if (result.hasErrors()) {
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        menuService.insetUpdateMenuHandler(updateDto, r);
        return r;
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/removeMenu", method = RequestMethod.GET)
    @ApiOperation("删除菜单")
    @MarkLog(operate = OperaTypeEnum.DELETE, tables = TableEnum.SYS_MENU, desc = "管理员删除菜单")
    public Result removeMenu(@RequestParam("id") Integer id) {
        Result r = new Result();
        if (id == null) {
            r.setCode(400).setMsg("id不能为空");
            return r;
        }
        menuService.removeMenu(id, r);
        return r;
    }
    /**
     * 获得菜单的树形结构
     * @return
     */
    @RequestMapping(value = "/getMenuTreeList",method = RequestMethod.GET)
    @ApiOperation("获得菜单的树形结构")
    public Result getMenuTreeList() {
        Result r = new Result();
        menuService.getMenuTreeList(r);
        return r;
    }
}

